package com.ruoyi.qbms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.qbms.domain.dto.QbmsQuestionChoiceImportDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.qbms.domain.QbmsQuestionChoice;
import com.ruoyi.qbms.service.IQbmsQuestionChoiceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 题库选择题Controller
 * 
 * @author zouhuu
 * @date 2022-10-17
 */
@RestController
@RequestMapping("/qbms/qbmsQuestionChoice")
public class QbmsQuestionChoiceController extends BaseController
{
    @Autowired
    private IQbmsQuestionChoiceService qbmsQuestionChoiceService;

    /**
     * 查询题库选择题列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsQuestionChoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(QbmsQuestionChoice qbmsQuestionChoice)
    {
        startPage();
        List<QbmsQuestionChoice> list = qbmsQuestionChoiceService.selectQbmsQuestionChoiceList(qbmsQuestionChoice);
        return getDataTable(list);
    }

    /**
     * 导出题库选择题列表
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsQuestionChoice:export')")
    @Log(title = "题库选择题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QbmsQuestionChoice qbmsQuestionChoice)
    {
        List<QbmsQuestionChoice> list = qbmsQuestionChoiceService.selectQbmsQuestionChoiceList(qbmsQuestionChoice);
        ExcelUtil<QbmsQuestionChoice> util = new ExcelUtil<QbmsQuestionChoice>(QbmsQuestionChoice.class);
        util.exportExcel(response, list, "题库选择题数据");
    }

    /**
     * 获取题库选择题详细信息
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsQuestionChoice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qbmsQuestionChoiceService.selectQbmsQuestionChoiceById(id));
    }

    /**
     * 新增题库选择题
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsQuestionChoice:add')")
    @Log(title = "题库选择题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QbmsQuestionChoice qbmsQuestionChoice)
    {
        return toAjax(qbmsQuestionChoiceService.insertQbmsQuestionChoice(qbmsQuestionChoice));
    }

    /**
     * 修改题库选择题
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsQuestionChoice:edit')")
    @Log(title = "题库选择题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QbmsQuestionChoice qbmsQuestionChoice)
    {
        return toAjax(qbmsQuestionChoiceService.updateQbmsQuestionChoice(qbmsQuestionChoice));
    }

    /**
     * 删除题库选择题
     */
    @PreAuthorize("@ss.hasPermi('qbms:qbmsQuestionChoice:remove')")
    @Log(title = "题库选择题", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qbmsQuestionChoiceService.deleteQbmsQuestionChoiceByIds(ids));
    }

    @Log(title = "题库管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, Long categoryId) throws Exception {
        ExcelUtil<QbmsQuestionChoiceImportDto> util = new ExcelUtil<QbmsQuestionChoiceImportDto>(QbmsQuestionChoiceImportDto.class);
        List<QbmsQuestionChoiceImportDto> questionImportList = util.importExcel(file.getInputStream());
        String msg = qbmsQuestionChoiceService.importQuestionChoice(questionImportList, categoryId);
        return AjaxResult.success(msg);
    }

    @PostMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<QbmsQuestionChoiceImportDto> util = new ExcelUtil<QbmsQuestionChoiceImportDto>(QbmsQuestionChoiceImportDto.class);
        return util.importTemplateExcel("题库数据");
    }
}
